/*******************************************************************************
 * Copyright (c) 2000, 2003 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
module dwt.ole.win32.oleframe;


private import dwt.dwt;


private import dwt.ole.win32.ole;
private import dwt.ole.win32.oleclientsite;

private import dwt.internal.ole.win32.com;
private import dwt.internal.ole.win32.OLEIDL;
private import dwt.internal.ole.win32.extras;
private import dwt.widgets.composite;
private import dwt.widgets.control;
private import dwt.widgets.display;
private import dwt.widgets.event;
private import dwt.widgets.listener;
private import dwt.widgets.menu;
private import dwt.widgets.menuitem;
private import dwt.widgets.shell;
private import dwt.widgets.widget;


private import dwt.util.javatypes;
private import dwt.util.util;
private import dwt.util.vector;

private import dwt.internal.ole.win32.ifs;


//	implements IOleInPlaceFrame, IOleInPlaceUIWindow, IOleWindow, IUnknown
class _IOleInPlaceFrameImpl : IOleInPlaceFrame {

	private import std.c.windows.windows;
	private import std.c.windows.com;

	OleFrame parent;
	this(OleFrame p) { parent = p; }
extern (Windows) :
	// interface of IUnknown
	HRESULT QueryInterface(REFIID riid, void ** ppvObject) { return parent.QueryInterface(riid, ppvObject); }
	ULONG AddRef()	{ return parent.AddRef(); }
	ULONG Release()	{ return parent.Release(); }

	// interface IOleWindow
	HRESULT GetWindow( HWND * phwnd ) { return parent.GetWindow(phwnd); }
    HRESULT ContextSensitiveHelp( BOOL fEnterMode ){ return COM.S_OK; }

	//interface IOleInPlaceUIWindow
	HRESULT GetBorder( LPRECT lprectBorder ) { return parent.GetBorder(lprectBorder); }
	HRESULT RequestBorderSpace( LPCBORDERWIDTHS pborderwidths ){ return COM.S_OK; }
	HRESULT SetBorderSpace( LPCBORDERWIDTHS pborderwidths ) { return parent.SetBorderSpace(pborderwidths); }
	HRESULT SetActiveObject( LPOLEINPLACEACTIVEOBJECT pActiveObject, LPCOLESTR pszObjName ) {
		return parent.SetActiveObject(pActiveObject, pszObjName);
	}

	// interface IOleInPlaceFrame : IOleInPlaceUIWindow
	HRESULT InsertMenus( HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths ){
		return parent.InsertMenus(hmenuShared, lpMenuWidths);
	}
	HRESULT SetMenu( HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject ){
		return parent.SetMenu(hmenuShared, holemenu, hwndActiveObject);
	}
	HRESULT RemoveMenus( HMENU hmenuShared ) {
		return parent.RemoveMenus(hmenuShared);
	}
	HRESULT SetStatusText( LPCOLESTR pszStatusText ) { return COM.E_NOTIMPL; }
	HRESULT EnableModeless( BOOL fEnable ) { return COM.E_NOTIMPL; }
	HRESULT TranslateAccelerator( LPMSG lpmsg, WORD wID ) {
		return parent.TranslateAccelerator(lpmsg, wID);
	}

}

/**
 *
 * OleFrame is an OLE Container's top level frame.
 *
 * <p>This object implements the OLE Interfaces IUnknown and IOleInPlaceFrame
 *
 * <p>OleFrame allows the container to do the following: <ul>
 *	<li>position and size the ActiveX Control or OLE Document within the application
 *	<li>insert menu items from the application into the OLE Document's menu
 *	<li>activate and deactivate the OLE Document's menus
 *	<li>position the OLE Document's menu in the application
 *	<li>translate accelerator keystrokes intended for the container's frame</ul>
 *
 * <dl>
 *	<dt><b>Styles</b> <dd>BORDER
 *	<dt><b>Events</b> <dd>Dispose, Move, Resize
 * </dl>
 *
 */
public class OleFrame : Composite
{

	private import std.c.windows.windows;
	private import std.c.windows.com;


	// Interfaces for this Ole Client Container
	private _IOleInPlaceFrameImpl iOleInPlaceFrame;

	// Access to the embedded/linked Ole Object
	private IOleInPlaceActiveObject objIOleInPlaceActiveObject;

	private OleClientSite currentdoc;

	private int refCount = 0;

	private MenuItem[] fileMenuItems;
	private MenuItem[] containerMenuItems;
	private MenuItem[] windowMenuItems;

	private Listener listener;

	private static char[] CHECK_FOCUS = "OLE_CHECK_FOCUS"; //$NON-NLS-1$
	private static char[] HHOOK_NAME = "OLE_HHOOK"; //$NON-NLS-1$
	private static char[] HHOOKMSG = "OLE_HHOOK_MSG"; //$NON-NLS-1$


	static Runnable timer;
	const int time = 50;
	static MSG sMsg;
	static Control lastFocus;


/**
 * Create an OleFrame child widget using style bits
 * to select a particular look or set of properties.
 *
 * @param parent a composite widget (cannot be null)
 * @param style the bitwise OR'ing of widget styles
 *
 * @exception SWTError
 * <ul><li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
 *     <li>ERROR_NULL_ARGUMENT when the parent is null
 *     <li>ERROR_INTERFACES_NOT_INITIALIZED when unable to create callbacks for OLE Interfaces</ul>
 *
 */
public this(Composite parent, int style) {
	// set up widget attributes
	super(parent, style | DWT.CLIP_CHILDREN | DWT.CLIP_SIBLINGS);

	createCOMInterfaces();

	// setup cleanup proc
	class LS1 : Listener{
		OleFrame c;
		public this(OleFrame o) {c=o;}
		public void handleEvent(Event e) {
			switch (e.type) {
			case DWT.Dispose :  c.onDispose(e); break;
			case DWT.Resize :
			case DWT.Move :     c.onResize(e); break;
			default :
				OLE.error (__FILE__, __LINE__, DWT.ERROR_NOT_IMPLEMENTED);
			}
		}
	}
	listener = new LS1(this)  ;

	addListener(DWT.Dispose, listener);

	// inform inplaceactiveobject whenever frame resizes
	addListener(DWT.Resize, listener);

	// inform inplaceactiveobject whenever frame moves
	addListener(DWT.Move, listener);

	// Maintain a reference to yourself so that when
	// ClientSites close, they don't take the frame away
	// with them.
	this.AddRef();

	// Check for focus change
	Display display = getDisplay();
	initCheckFocus(display);
	initMsgHook(display);
}

HRESULT QueryInterface(REFIID riid, void ** ppvObject) {
//	implements IUnknown, IOleInPlaceFrame, IOleContainer, IOleInPlaceUIWindow

	if (riid is null || ppvObject is null)
		return COM.E_INVALIDARG;

	if (*riid == COM.IIDIUnknown) {
		*ppvObject = cast(void*)(cast(IUnknown)iOleInPlaceFrame);
		AddRef();
		return COM.S_OK;
	}

	if(*riid == COM.IIDIOleInPlaceFrame ) {
		*ppvObject = cast(void*)(cast(IOleInPlaceFrame)iOleInPlaceFrame);
		AddRef();
		return COM.S_OK;
	}
	*ppvObject = null;
	return COM.E_NOINTERFACE;
}
/**
 * Increment the count of references to this instance
 *
 * @return the current reference count
 */
ULONG AddRef(){
	return OS.InterlockedIncrement(&refCount);
}

/**
 * Decrement the count of references to this instance
 *
 * @return the current reference count
 */
ULONG Release(){
	int ref1 = OS.InterlockedDecrement(&refCount);
	if(ref1 == 0){
		disposeCOMInterfaces();
		COM.CoFreeUnusedLibraries();
	}
	return ref1;
}

private static void initCheckFocus (Display display) {
	if (display.getData(CHECK_FOCUS) !is null) return;

	display.setData(CHECK_FOCUS, new StringObj(CHECK_FOCUS));

	class Run1 : Runnable{
		Control lastFocus;
		Display display;
		public this(Display d){
			display = d;
			lastFocus =  null;
		}
		public void run() {
			if (cast(OleClientSite)lastFocus && !lastFocus.isDisposed()) {
				// ignore popup menus and dialogs
				HWND hwnd = OS.GetFocus();
				while (hwnd !is null) {
					HWND ownerHwnd = OS.GetWindow(hwnd, 4/*GW_OWNER*/);
					if (ownerHwnd !is null) {
						display.timerExec(time, OleFrame.timer);
						return;
					}
					hwnd = OS.GetParent(hwnd);
				}
			}
			if (lastFocus is null || lastFocus.isDisposed() || !lastFocus.isFocusControl()) {
				Control currentFocus = display.getFocusControl();
				if (cast(OleFrame)currentFocus) {
					OleFrame frame = cast(OleFrame)currentFocus;
					currentFocus = frame.getCurrentDocument();
				}
				if (lastFocus !is currentFocus) {
					Event event = new Event();
					if ( cast(OleClientSite)lastFocus && !lastFocus.isDisposed()) {
						lastFocus.notifyListeners (DWT.FocusOut, event);
					}
					if (cast(OleClientSite)currentFocus && !currentFocus.isDisposed()) {
						currentFocus.notifyListeners(DWT.FocusIn, event);
					}
				}
				lastFocus = currentFocus;
			}
			display.timerExec(time, timer);
		}
	}
	timer = new Run1(display);
	display.timerExec(time, timer);
}

extern (Windows) static
private int getMsgProc(int code, int wParam, int lParam) {
	Display display = Display.getCurrent();
	if (display is null) return 0;
	Integer hHook = cast(Integer)display.getData(HHOOK_NAME);
	if (hHook is null) return 0;
	if (code < 0) {
		return OS.CallNextHookEx(cast(HWND)hHook.intValue(), code, wParam, lParam);
	}
	MSG* msg = cast(MSG*)display.getData(HHOOKMSG);
	OS.MoveMemory(msg, lParam, MSG.sizeof);
	int message = msg.message;
	if (OS.WM_KEYFIRST <= message && message <= OS.WM_KEYLAST) {
		if (display !is null) {
			Widget widget = null;
			HWND hwnd = msg.hwnd;
			while (hwnd !is null) {
				widget = display.findWidget (hwnd);
				if (widget !is null) break;
				hwnd = OS.GetParent (hwnd);
			}
			if (widget !is null && cast(OleClientSite)widget) {
				OleClientSite site = cast(OleClientSite)widget;
				if (site.handle is hwnd) {
					/*
					 * Do not allow the activeX control to translate events
					 * when a menu is active.
					 */
					int thread = OS.GetWindowThreadProcessId(msg.hwnd, null);
					GUITHREADINFO  lpgui;
					lpgui.cbSize = GUITHREADINFO.sizeof;
					boolean rc = OS.GetGUIThreadInfo(thread, &lpgui);
					int mask = OS.GUI_INMENUMODE | OS.GUI_INMOVESIZE | OS.GUI_POPUPMENUMODE | OS.GUI_SYSTEMMENUMODE;
					if (!rc || (lpgui.flags & mask) == 0) {
						OleFrame frame = site.frame;
						if (frame.translateOleAccelerator(msg)) {
							// In order to prevent this message from also being processed
							// by the application, zero out message, wParam and lParam
							msg.message = OS.WM_NULL;
							msg.wParam = 0;
							msg.lParam = 0;
							OS.MoveMemory(lParam, msg, MSG.sizeof);
							return 0;
						}
					}
				}
			}
		}
	}
	return OS.CallNextHookEx(cast(HWND)hHook.intValue(), code, wParam, lParam);
}

private static void initMsgHook(Display display) {

	if (display.getData(HHOOK_NAME) !is null) return;

	int threadId = OS.GetCurrentThreadId();
	HHOOK hHook = OS.SetWindowsHookEx(OS.WH_GETMESSAGE, &getMsgProc, null, threadId);
	if (hHook is null) {
		return;
	}
	display.setData(HHOOK_NAME, new Integer(cast(int)hHook));

	display.setData(HHOOKMSG, cast(Object)&sMsg);

	class Run1 : Runnable {
		HHOOK hHook;
		public this(HHOOK i){hHook = i;}
		public void run() {
			if (hHook !is null) OS.UnhookWindowsHookEx(hHook);
		}
	}
	display.disposeExec(new Run1(hHook));
}

protected void createCOMInterfaces() {
	iOleInPlaceFrame = new _IOleInPlaceFrameImpl(this);
}
protected void disposeCOMInterfaces () {
	iOleInPlaceFrame = null;
}

HRESULT GetBorder( LPRECT lprectBorder ) {
	/*
	The IOleInPlaceUIWindow::GetBorder function, when called on a document or frame window
	object, returns the outer rectangle (relative to the window) where the object can put
	toolbars or similar controls.
	*/
	if (lprectBorder is null) return COM.E_INVALIDARG;

	// Coordinates must be relative to the window
	OS.GetClientRect(handle, lprectBorder);
	return COM.S_OK;
}

/**
 *
 * Returns the application menu items that will appear in the Container location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * @return the application menu items that will appear in the Container location when an OLE Document
 *         is in-place activated.
 *
 */
public MenuItem[] getContainerMenus(){
	return containerMenuItems;
}
/**
 *
 * Returns the application menu items that will appear in the File location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * @return the application menu items that will appear in the File location when an OLE Document
 *         is in-place activated.
 *
 */
public MenuItem[] getFileMenus(){
	return fileMenuItems;
}
IOleInPlaceFrame getIOleInPlaceFrame() {
	return iOleInPlaceFrame;
}
private int getMenuItemID(HMENU hMenu, int index) {
	int id = 0;
	MENUITEMINFO lpmii;
	lpmii.cbSize = MENUITEMINFO.sizeof;
	lpmii.fMask = OS.MIIM_STATE | OS.MIIM_SUBMENU | OS.MIIM_ID;
	OS.GetMenuItemInfo(hMenu, index, true, &lpmii);
	if ((lpmii.fState & OS.MF_POPUP) == OS.MF_POPUP) {
		id = cast(int)lpmii.hSubMenu;
	} else {
		id = lpmii.wID;
	}
	return id;
}

HRESULT GetWindow( HWND * phwnd ){
	if (phwnd !is null)
		*phwnd = handle;
	return COM.S_OK;
}
/**
 *
 * Returns the application menu items that will appear in the Window location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * @return the application menu items that will appear in the Window location when an OLE Document
 *         is in-place activated.
 *
 */
public MenuItem[] getWindowMenus(){
	return windowMenuItems;
}

HRESULT InsertMenus( HMENU hmenuShared, LPOLEMENUGROUPWIDTHS lpMenuWidths ) {
	// locate menu bar
	Menu menubar = getShell().getMenuBar();
	if (menubar is null || menubar.isDisposed()) {
		int temp = 0;
		COM.MoveMemory(lpMenuWidths, &temp, 4);
		return COM.S_OK;
	}
	HMENU hMenu = menubar.handle;

	// Create a holder for menu information.  This will be passed down to
	// the OS and the OS will fill in the requested information for each menu.
	MENUITEMINFO lpmii;
	int cch = 128;
	TCHAR[] pszText = new TCHAR[cch];
	lpmii.cbSize = MENUITEMINFO.sizeof;
	lpmii.fMask = OS.MIIM_STATE | OS.MIIM_ID | OS.MIIM_TYPE | OS.MIIM_SUBMENU | OS.MIIM_DATA;
	lpmii.dwTypeData = pszText.ptr;
	lpmii.cch = cch;

	// Loop over all "File-like" menus in the menubar and get information about the
	// item from the OS.
	int fileMenuCount = 0;
	int newindex = 0;
	if (fileMenuItems !is null) {
		for (int i = 0; i < fileMenuItems.length; i++) {
			MenuItem item = fileMenuItems[i];
			if (item !is null) {
				int index = item.getParent().indexOf(item);
				lpmii.cch = cch;  // lpmii.cch gets updated by GetMenuItemInfo to indicate the
				                  // exact number of characters in name.  Reset it to our max size
				                  // before each call.
				if (OS.GetMenuItemInfo(hMenu, index, true, &lpmii)) {
					if (OS.InsertMenuItem(hmenuShared, newindex, true, &lpmii)) {
						// keep track of the number of items
						fileMenuCount++;
						newindex++;
					}
				}
			}
		}
	}

	// copy the menu item count information to the pointer
	COM.MoveMemory(lpMenuWidths, &fileMenuCount, 4);

	// Loop over all "Container-like" menus in the menubar and get information about the
	// item from the OS.
	int containerMenuCount = 0;
	if (containerMenuItems !is null) {
		for (int i = 0; i < containerMenuItems.length; i++) {
			MenuItem item = containerMenuItems[i];
			if (item !is null) {
				int index = item.getParent().indexOf(item);
				lpmii.cch = cch; // lpmii.cch gets updated by GetMenuItemInfo to indicate the
				                           // exact nuber of characters in name.  Reset it to a large number
				                           // before each call.
				if (OS.GetMenuItemInfo(hMenu, index, true, &lpmii)) {
					if (OS.InsertMenuItem(hmenuShared, newindex, true, &lpmii)) {
						// keep track of the number of items
						containerMenuCount++;
						newindex++;
					}
				}
			}
		}
	}

	// copy the menu item count information to the pointer
	COM.MoveMemory(lpMenuWidths + 8, &containerMenuCount, 4);

	// Loop over all "Window-like" menus in the menubar and get information about the
	// item from the OS.
	int windowMenuCount = 0;
	if (windowMenuItems !is null) {
		for (int i = 0; i < windowMenuItems.length; i++) {
			MenuItem item = windowMenuItems[i];
			if (item !is null) {
				int index = item.getParent().indexOf(item);
				lpmii.cch = cch; // lpmii.cch gets updated by GetMenuItemInfo to indicate the
				                           // exact nuber of characters in name.  Reset it to a large number
				                           // before each call.
				if (OS.GetMenuItemInfo(hMenu, index, true, &lpmii)) {
					if (OS.InsertMenuItem(hmenuShared, newindex, true, &lpmii)) {
						// keep track of the number of items
						windowMenuCount++;
						newindex++;
					}
				}
			}
		}
	}

	// copy the menu item count information to the pointer
	COM.MoveMemory(lpMenuWidths + 16, &windowMenuCount, 4);

	return COM.S_OK;
}

package void onDispose(Event e) {

	releaseObjectInterfaces();
	currentdoc = null;

	this.Release();
	removeListener(DWT.Dispose, listener);
	removeListener(DWT.Resize, listener);
	removeListener(DWT.Move, listener);
}
package void onResize(Event e) {
	if (objIOleInPlaceActiveObject !is null) {
		RECT lpRect;
		OS.GetClientRect(handle, &lpRect);
		objIOleInPlaceActiveObject.ResizeBorder(&lpRect, iOleInPlaceFrame, true);
	}
}

private void releaseObjectInterfaces() {
	if (objIOleInPlaceActiveObject !is null) {
		objIOleInPlaceActiveObject.Release();
	}
	objIOleInPlaceActiveObject = null;
}

HRESULT RemoveMenus( HMENU hmenuShared ){

	Menu menubar = parent.getShell().getMenuBar();
	if (menubar is null || menubar.isDisposed()) return COM.S_FALSE;

	HMENU hMenu = menubar.handle;

	Vector!(Integer) ids = new Vector!(Integer);
	if (fileMenuItems !is null) {
		for (int i = 0; i < fileMenuItems.length; i++) {
			MenuItem item = fileMenuItems[i];
			if (item !is null && !item.isDisposed()) {
				int index = item.getParent().indexOf(item);
				// get Id from original menubar
				int id = getMenuItemID(hMenu, index);
				ids.addElement(new Integer(id));
			}
		}
	}
	if (containerMenuItems !is null) {
		for (int i = 0; i < containerMenuItems.length; i++) {
			MenuItem item = containerMenuItems[i];
			if (item !is null && !item.isDisposed()) {
				int index = item.getParent().indexOf(item);
				int id = getMenuItemID(hMenu, index);
				ids.addElement(new Integer(id));
			}
		}
	}
	if (windowMenuItems !is null) {
		for (int i = 0; i < windowMenuItems.length; i++) {
			MenuItem item = windowMenuItems[i];
			if (item !is null && !item.isDisposed()) {
				int index = item.getParent().indexOf(item);
				int id = getMenuItemID(hMenu, index);
				ids.addElement(new Integer(id));
			}
		}
	}
	int index = OS.GetMenuItemCount(hmenuShared) - 1;
	for (int i = index; i >= 0; i--) {
		int id = getMenuItemID(hmenuShared, i);
		if (ids.contains(new Integer(id))){
			OS.RemoveMenu(hmenuShared, i, OS.MF_BYPOSITION);
		}
	}
	return COM.S_OK;
}

HRESULT SetActiveObject( LPOLEINPLACEACTIVEOBJECT pActiveObject, LPCOLESTR pszObjName ) {
	if (objIOleInPlaceActiveObject !is null) {
		objIOleInPlaceActiveObject.Release();
		objIOleInPlaceActiveObject = null;
	}
	if (pActiveObject !is null) {
		objIOleInPlaceActiveObject = (pActiveObject);
		objIOleInPlaceActiveObject.AddRef();
	}

	return COM.S_OK;
}

HRESULT SetBorderSpace( LPCBORDERWIDTHS pborderwidths ) {
	// A Control/Document can :
	// Use its own toolbars, requesting border space of a specific size, or,
	// Use no toolbars, but force the container to remove its toolbars by passing a
	//   valid BORDERWIDTHS structure containing nothing but zeros in the pborderwidths parameter, or,
	// Use no toolbars but allow the in-place container to leave its toolbars up by
	//   passing NULL as the pborderwidths parameter.
	if (objIOleInPlaceActiveObject is null) return COM.S_OK;
	RECT borderwidth;
	if (pborderwidths is null || currentdoc is null ) return COM.S_OK;

	COM.MoveMemory(&borderwidth, pborderwidths, RECT.sizeof);
	currentdoc.setBorderSpace(&borderwidth);

	return COM.S_OK;
}
/**
 *
 * Specify the menu items that should appear in the Container location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * <p>This method must be called before in place activation of the OLE Document.  After the Document
 * is activated, the menu bar will not be modified until a subsequent activation.
 *
 * @param containerMenus an array of top level MenuItems to be inserted into the Container location of
 *        the menubar
 */
public void setContainerMenus(MenuItem[] containerMenus){
	containerMenuItems = containerMenus;
}
OleClientSite getCurrentDocument() {
	return currentdoc;
}
void setCurrentDocument(OleClientSite doc) {
	currentdoc = doc;

	if (currentdoc !is null && objIOleInPlaceActiveObject !is null) {
		RECT lpRect;
		OS.GetClientRect(handle, &lpRect);
		objIOleInPlaceActiveObject.ResizeBorder(&lpRect, iOleInPlaceFrame, true);
	}
}

/**
 *
 * Specify the menu items that should appear in the File location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * <p>This method must be called before in place activation of the OLE Document.  After the Document
 * is activated, the menu bar will not be modified until a subsequent activation.
 *
 * @param fileMenus an array of top level MenuItems to be inserted into the Flie location of
 *        the menubar
 */
public void setFileMenus(MenuItem[] fileMenus){
	fileMenuItems = fileMenus;
}

HRESULT SetMenu( HMENU hmenuShared, HOLEMENU holemenu, HWND hwndActiveObject ) {
	IOleInPlaceActiveObject inPlaceActiveObject;
	if (objIOleInPlaceActiveObject !is null)
		inPlaceActiveObject = objIOleInPlaceActiveObject;

	Menu menubar = getShell().getMenuBar();
	if (menubar is null || menubar.isDisposed()){
		return COM.OleSetMenuDescriptor(null, getShell().handle, hwndActiveObject, iOleInPlaceFrame, inPlaceActiveObject);
	}

	HWND handle = menubar.getShell().handle;

	if (hmenuShared is null && holemenu is null) {
		// re-instate the original menu - this occurs on deactivation
		hmenuShared = menubar.handle;
	}
	if (hmenuShared is null) return COM.E_FAIL;

	OS.SetMenu(handle, hmenuShared);
	OS.DrawMenuBar(handle);

	return COM.OleSetMenuDescriptor(holemenu, handle, hwndActiveObject, iOleInPlaceFrame, inPlaceActiveObject);
}


/**
 *
 * Set the menu items that should appear in the Window location when an OLE Document
 * is in-place activated.
 *
 * <p>When an OLE Document is in-place active, the Document provides its own menus but the application
 * is given the opportunity to merge some of its menus into the menubar.  The application
 * is allowed to insert its menus in three locations: File (far left), Container(middle) and Window
 * (far right just before Help).  The OLE Document retains control of the Edit, Object and Help
 * menu locations.  Note that an application can insert more than one menu into a single location.
 *
 * <p>This method must be called before in place activation of the OLE Document.  After the Document
 * is activated, the menu bar will not be modified until a subsequent activation.
 *
 * @param windowMenus an array of top level MenuItems to be inserted into the Window location of
 *        the menubar
 */
public void setWindowMenus(MenuItem[] windowMenus){
	windowMenuItems = windowMenus;
}
private boolean translateOleAccelerator(MSG* msg) {
	if (objIOleInPlaceActiveObject is null) return false;
	int result = objIOleInPlaceActiveObject.TranslateAccelerator(msg);
	return (result != COM.S_FALSE && result != COM.E_NOTIMPL);
}

HRESULT TranslateAccelerator( LPMSG lpmsg, WORD wID ){
	Menu menubar = getShell().getMenuBar();
	if (menubar is null || menubar.isDisposed() || !menubar.isEnabled()) return COM.S_FALSE;
	if (wID < 0) return COM.S_FALSE;

	Shell shell = menubar.getShell();
	HWND hwnd = shell.handle;
	HACCEL hAccel = cast(HACCEL)OS.SendMessage(hwnd, OS.WM_APP+1, 0, 0);
	if (hAccel is null) return COM.S_FALSE;

	MSG msg = *lpmsg;
	int result = OS.TranslateAccelerator(hwnd, hAccel, &msg);
	return result == 0 ? COM.S_FALSE : COM.S_OK;
}

}
